﻿Imports System.Web.UI
Imports System.Reflection

Namespace DotNetNuke.Modules.ActiveForums
    Public Class Environment
        Public Shared Function UpdateBreadCrumb(ByVal ctrls As ControlCollection, ByVal ForumBread As String) As Boolean
            If String.IsNullOrEmpty(ForumBread) Then
                Return True
            End If
            Dim bcText() As String = ForumBread.Split(CChar("|"))
            Try
                For Each ctrl As Control In ctrls
                    If TypeOf ctrl Is DotNetNuke.UI.Skins.SkinObjectBase AndAlso Not ctrl.TemplateControl.AppRelativeVirtualPath Is Nothing Then
                        If ctrl.TemplateControl.AppRelativeVirtualPath.ToLowerInvariant.Contains("breadcrumb.ascx") Then
                            Dim o As Object = ctrl.GetType().GetProperty("Separator").GetValue(ctrl, BindingFlags.Public Or BindingFlags.NonPublic, Nothing, Nothing, Nothing)
                            Dim cssObject As Object = ctrl.GetType().GetProperty("CssClass").GetValue(ctrl, BindingFlags.Public Or BindingFlags.NonPublic, Nothing, Nothing, Nothing)
                            Dim css As String = "SkinObject"
                            If Not cssObject Is Nothing Then
                                If Not String.IsNullOrEmpty(cssObject.ToString()) Then
                                    css = cssObject.ToString()
                                End If
                            End If
                            

                            Dim sText As String = o.ToString
                            Dim sBread As String = String.Empty
                            For Each s As String In bcText
                                If Not String.IsNullOrEmpty(s) Then
                                    s = s.Replace("<a ", "<a class=""" & css & """")
                                    sBread &= sText & s
                                End If
                            Next
                            DirectCast(ctrl.FindControl("lblBreadCrumb"), WebControls.Label).Text &= sBread
                            Exit For
                        End If
                    End If
                    If ctrl.Controls.Count > 0 Then
                        UpdateBreadCrumb(ctrl.Controls, ForumBread)
                    End If
                Next
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function
        Public Shared Function UpdateMeta(ByRef bp As DotNetNuke.Framework.CDefault, ByVal Title As String, ByVal Description As String, ByVal Keywords As String) As Boolean
            If bp Is Nothing Then
                Return False
            End If
            Try
                If Not String.IsNullOrEmpty(Title) Then
                    If Not String.IsNullOrEmpty(bp.Title) Then
                        bp.Title = Title.Replace("[VALUE]", bp.Title)
                    Else
                        bp.Title = Title.Replace("[VALUE]", String.Empty)
                    End If
                End If
                If Not String.IsNullOrEmpty(Description) Then
                    Description = Description.Replace(vbCrLf, " ")
                    If Not String.IsNullOrEmpty(bp.Description) Then
                        bp.Description = Description.Replace("[VALUE]", bp.Description)
                    Else
                        bp.Description = Description.Replace("[VALUE]", String.Empty)
                    End If
                End If
                If Not String.IsNullOrEmpty(Keywords) Then
                    If Not String.IsNullOrEmpty(bp.KeyWords) Then
                        Dim cKey As String = bp.KeyWords.Trim
                        If cKey.StartsWith(",") Then
                            cKey = cKey.Substring(1)
                        ElseIf cKey.EndsWith(",") Then
                            cKey = cKey.Substring(0, cKey.Length - 1)
                        End If
                        If Keywords.StartsWith("[VALUE]") Then
                            cKey &= ","
                        ElseIf Keywords.EndsWith("[VALUE]") Then
                            cKey = "," & cKey
                        End If
                        bp.KeyWords = Keywords.Replace("[VALUE]", cKey)
                    Else
                        bp.KeyWords = Keywords.Replace("[VALUE]", String.Empty)
                    End If
                End If
            Catch ex As Exception
                Return False
            End Try
        End Function
    End Class
End Namespace
